﻿using System;
namespace Clock;


class program
{
    class Clock
    {
        DateTime alarmTime = DateTime.Now;

        public delegate void TickHandler(object sender, DateTime args);
        public delegate void AlarmHandler(object sender, DateTime args);

        public event TickHandler Tick;
        public event AlarmHandler Alarm;

        public Clock()
        {
            Tick += useTick;
            Alarm += useAlarm;
        }

        public void useTick(object sender, DateTime time)
        {
            Console.WriteLine("Tick!现在是：" + time);
        }

        public void useAlarm(object sender, DateTime time)
        {
            Console.WriteLine("Alarm!" + time + "到了");
        }

        public void Start()
        {
            while (true)
            {
                DateTime now = DateTime.Now;
                Tick(this, now);
                if (now.ToString() == alarmTime.ToString())//tick事件始终发生
                {
                    Alarm(this, alarmTime);
                }
                System.Threading.Thread.Sleep(1000);//时钟休眠1000ns，即每秒重新获取事件
            }
        }

        public void SetAlarmTime(DateTime atime)
        {
            Console.WriteLine(atime);//定时
            alarmTime = atime;
        }
    }
    static void Main(string[] args)
    {
        Clock clock = new Clock();
        DateTime atime = new DateTime();
        atime = DateTime.Now.AddSeconds(5);//定时，在时间基础上+10秒
        clock.SetAlarmTime(atime);
        clock.Start();
    }
}
